package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.message.adapter.MyGroupChatAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.enums.GroupChatIdentity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/13
 **/
public class MyGroupChatActivity extends BaseActivityImpl {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private MyGroupChatAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public String getTitleStr() {
        return "我的群聊";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new MyGroupChatAdapter(mContext, new Consumer<ContactGroup>() {
            @Override
            public void accept(ContactGroup contactGroup) {
                if (contactGroup.getGroup_type() == 1) { // 群聊类型 1:家庭 2普通
                    ChatMessageActivity.show(mContext, contactGroup);
                } else {
                    GroupChatIdentity groupChatIdentity = contactGroup.getGroupChatIdentity();
                    switch (groupChatIdentity) {
                        case MEMBER:
                        case GROUP_LEADER:
                            HashMap<String, Object> params = new HashMap<>();
                            params.put("id", contactGroup.getId());
                            params.put("is_open", !contactGroup.isIs_open());
                            requestManager.request(requestManager.mMessageRetrofitService.editGroupChatSetting(RequestManager.getJsonRequestBody(params)),
                                    new BaseObserverImpl<Object>(mContext) {

                                        @Override
                                        public void onSuccess() {
                                            super.onSuccess();

                                            refresh();
                                        }
                                    });
                            break;
                    }
                }
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        recyclerView.setAdapter(adapter);

        refresh();
    }

    private void refresh() {
        requestManager.request(requestManager.mMessageRetrofitService.getGroupList(),
                new BaseObserverImpl<ArrayList<ContactGroup>>(mContext) {
                    @Override
                    public void onSuccess(ArrayList<ContactGroup> contactGroups) {
                        super.onSuccess(contactGroups);
                        if (contactGroups.size() > 0) {
                            recyclerView.setVisibility(View.VISIBLE);
                            llNoData.setVisibility(View.GONE);

                            String[] types = new String[]{"家庭群", "已公开的群聊", "未公开的群聊"};

                            HashMap<String, ArrayList<ContactGroup>> map = new HashMap<>();

                            for (String type : types) {
                                map.put(type, new ArrayList<>());
                            }

                            contactGroups.forEach(new Consumer<ContactGroup>() {
                                @Override
                                public void accept(ContactGroup contactGroup) {
                                    if (contactGroup.getGroup_type() == 1) {
                                        map.get(types[0]).add(contactGroup);
                                    } else {
                                        if (contactGroup.isIs_open()) {
                                            map.get(types[1]).add(contactGroup);
                                        } else {
                                            map.get(types[2]).add(contactGroup);
                                        }
                                    }
                                }
                            });

                            ArrayList<Object> datas = new ArrayList<>();
                            for (String type : types) {
                                ArrayList<ContactGroup> list = map.get(type);
                                if (list.size() > 0) {
                                    datas.add(type);
                                    datas.addAll(list);
                                }
                            }
                            adapter.refresh(datas);
                        } else {
                            recyclerView.setVisibility(View.GONE);
                            llNoData.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }
}




























